#include <iostream>

using namespace std;

struct LinkNode {
    int a;
    LinkNode* next;
};

LinkNode* initLinkList() {
    LinkNode* pHead = new LinkNode{0, nullptr};
    LinkNode* pCurrent = pHead;

    int _a = 0;

    while (true)
    {
        cin >> _a;
        if (_a == -1) {
            cout << "Finished LinkList initlize..." << endl;
            return pHead;
        }
        LinkNode* pTail = new LinkNode;
        pTail->a = _a;
        pTail->next = nullptr;
        pCurrent->next = pTail;
        pCurrent = pTail;
    }
    return pHead;
}

void forEachLinkList(LinkNode* pHead) {

    if (pHead == nullptr) {
        cout << "Empty LinkList!" << endl;
        return;
    }
    LinkNode* pCurrent = pHead->next;

    int i = 1;
    cout << "ForEach LinkList:";
    while (pCurrent != nullptr) {
        // cout << i++ << " ::a = "<< pCurrent->a << " ";
        cout << pCurrent->a << ", ";
        pCurrent = pCurrent->next;
    }
    cout << endl;

}

void insertLinkList(LinkNode* pHead, int oldVal, int newVal) {
    if (pHead == nullptr) return;
    LinkNode* pCurrent = pHead->next;
    LinkNode* pPrev = pHead;
    // lookfor position
    while (pCurrent!=nullptr) { 
        if (pCurrent->a == oldVal) {
            break;
        }
        // if no match, auxiliary pointer moves back
        pPrev = pCurrent;
        pCurrent = pCurrent->next;
    }
    // insert
    LinkNode* newNode = new LinkNode{newVal, nullptr};
    newNode->next = pCurrent;
    pPrev->next = newNode;
}

void deleteLinkList(LinkNode* pHead, int val) {
    if (pHead == nullptr || pHead->next == nullptr) return;
    LinkNode* pPrev = pHead;
    LinkNode* pCurrent = pHead->next;

    while (pCurrent!=nullptr) {
        if (pCurrent->a == val) break;
        pPrev = pCurrent;
        pCurrent = pCurrent->next;
    }

    if (pCurrent == nullptr) return;
    pPrev->next = pCurrent->next;
    delete pCurrent;
    pCurrent = nullptr;
}

void clearLinkList(LinkNode* pHead) {
    if (pHead == nullptr) return;
    LinkNode* pCurrent = pHead->next;
    while (pCurrent != nullptr) {
        LinkNode* pNext = pCurrent->next;
        delete pCurrent;
        pCurrent = nullptr;
    }
    pHead->next = nullptr;
}

void destroyLinkList(LinkNode* pHead) {
    clearLinkList(pHead);
    delete pHead;
}

void test01() {
    cout << "init: 1, 5, 7" << endl;
    LinkNode* pHead = initLinkList();
    forEachLinkList(pHead);

    cout << "insert result: 1, 50, 5, 70, 7, -100" << endl;
    insertLinkList(pHead, 5, 50);
    insertLinkList(pHead, 7, 70);
    insertLinkList(pHead, -1, -100);
    forEachLinkList(pHead);

    cout << "delete result: 1, 50, 70, 7" << endl;
    deleteLinkList(pHead, 5);
    deleteLinkList(pHead, -100);
    deleteLinkList(pHead, -1);
    forEachLinkList(pHead);

    // cout << "clear result: " << endl;
    // clearLinkList(pHead);
    // forEachLinkList(pHead);

    cout << "destroy result: " << endl;
    destroyLinkList(pHead);
    pHead = nullptr;
    forEachLinkList(pHead);
}

int main() {
    test01();
    return 0;
}